package com.qilin.hammer;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class LogFetcher {
	

	private void writeLog(String destpath, ArrayList<String> results){
		
		BufferedWriter output;
		try {
			output = new BufferedWriter(new FileWriter(new File(destpath)));
			for(String s:results)
			   output.write(s+"\r\n");
			output.flush();
			output.close();
		} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		}

	}
	
	private void readTokenLog(String filepath, String destpath){
		try {
			BufferedReader br = new BufferedReader(new FileReader(filepath));
			String linetext = null;
			ArrayList<String> results = new ArrayList<String>();
			Matcher timer = null;
			Pattern p = Pattern.compile("\\d{4}-\\d{2}-\\d{2}\\b\\d{2}:\\d{2}:\\d{2}");
			while((linetext=br.readLine())!=null){
				if(linetext.indexOf("processToken()")==-1&&linetext.indexOf("Failed to create principal")==-1)
					continue;
				//timer = p.matcher(linetext);
			    results.add(linetext);
			}
			writeLog(destpath, results);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void handleLog(int beg, int end, String filepath){
		ArrayList<String> results = new ArrayList<String>();
		int p = 10;
		try{
			BufferedReader br = new BufferedReader(new FileReader(filepath));
			String linetext = null;
			while((linetext=br.readLine())!=null){
				//if( ( linetext.indexOf("AppHomeLib")!=-1 || linetext.indexOf("AppInstallManager")!=-1 )&& linetext.indexOf("[com.vitria.m3oui.model.PerformanceLogger]")!=-1){
					results.add(linetext);
				//}
			}
		}catch (IOException e) {
			e.printStackTrace();
		}
		if(end < beg|| results.size()%p!=0)
			return;
		int total = end - beg +1;
		int sum1=0,sum2=0,sum3=0,sum4=0,sum5=0,sum6=0,sum7=0,sum8=0, pre1sum=0, pre2sum=0;
		for(int i=beg; i<=end; i++){
			int a1=0, a2=0, a3=0, a4=0, a5=0, a6=0, a7=0;
			int pre1=0, pre2=0;
			String linetext = null;
			for(int j = (i-1)*p; j<(i-1)*p+p; j++){
				linetext = results.get(j);
				int right, left;
				right = linetext.lastIndexOf(" ");
				left = linetext.lastIndexOf(" ", right-1);
				switch(j%p){
					case 0:
						if(linetext.indexOf("installApp")==-1)
							return;
						a1 = Integer.parseInt(linetext.substring(left+1,right));
						System.out.println("install time is "+ a1);
						break;
					case 1:
						if(linetext.indexOf("enableApp")==-1)
							return;
						a2 = Integer.parseInt(linetext.substring(left+1,right));
						System.out.println("enableApp time is "+ a2);
						break;
					case 2:
						if(linetext.indexOf("disableApp")==-1)
							return;
						a3 = Integer.parseInt(linetext.substring(left+1,right));
						System.out.println("disableApp time is "+ a3);
						break;
					case 3:
						if(linetext.indexOf("exportApp")==-1)
							return;
						a4 = Integer.parseInt(linetext.substring(left+1,right));
						System.out.println("exportApp time is "+ a4);
						break;
					case 4:
						if(linetext.indexOf("preUninstallApp")==-1)
							return;
						pre1 = Integer.parseInt(linetext.substring(left+1,right));
						System.out.println("preUninstallApp time is "+ pre1);
						break;	
					case 5:
						if(linetext.indexOf("uninstallApp")==-1)
							return;
						a5 = Integer.parseInt(linetext.substring(left+1,right));
						System.out.println("uninstallApp time is "+ a5);
						break;
					case 6:
						if(linetext.indexOf("installApp")==-1)
							return;
						a6 = Integer.parseInt(linetext.substring(left+1,right));
						System.out.println("install time is "+ a6);
						break;
					case 7:
						if(linetext.indexOf("enableApp")==-1)
							return;
						break;
					case 8:
						if(linetext.indexOf("preUninstallApp")==-1)
							return;
						pre2 = Integer.parseInt(linetext.substring(left+1,right));
						System.out.println("preUninstallApp time is "+ pre2);
						break;
					case 9:
						if(linetext.indexOf("uninstallApp")==-1)
							return;
						a7 = Integer.parseInt(linetext.substring(left+1,right));
						System.out.println("uninstallApp time is "+ a7);
						break;
				}
			}
			//appLifePerfItem item = new appLifePerfItem(a1, a2, a3, a4, a5, a6, a7);
			sum1 += a1;
			sum2 += a2;
			sum3 += a3;
			sum4 += a4;
			sum5 += a5;
			sum6 += a6;
			sum7 += a5+a6;
			System.out.println("upgrade time is "+ (a5+a6+pre1));
			System.out.println("uninstall time is "+ (a7+pre2));
			sum8 += a7;
			pre1sum += pre1;
			pre2sum += pre2;
			System.out.println("华丽的分割线");
		}
		System.out.println("Total is "+total);
		System.out.println("Install avrage is "+sum1*1.0/total);
		System.out.println("Enable avrage is "+sum2*1.0/total);
		System.out.println("Disable avrage is "+sum3*1.0/total);
		System.out.println("Export avrage is "+sum4*1.0/total);
		System.out.println("Uninstall in upgrade avrage is "+sum5*1.0/total);
		System.out.println("Install in upgrade avrage is "+sum6*1.0/total);
		System.out.println("Upgrade without preuninstall avrage is "+sum7*1.0/total);
		System.out.println("Uninstall without preunisntall avrage is "+sum8*1.0/total);
		System.out.println("Upgrade with preuninstall avrage is "+(sum7+pre1sum)*1.0/total);
		System.out.println("Uninstall with preunisntall avrage is "+(sum8+pre2sum)*1.0/total);
		System.out.println("Preuninstall in upgrade avrage is "+pre1sum*1.0/total);
		System.out.println("Preuninstall in uninstall avrage is "+pre2sum*1.0/total);
	}
	
	private void readAppPerformanceLog(String filepath, String destpath){
		try{
			BufferedReader br = new BufferedReader(new FileReader(filepath));
			String linetext = null;
			ArrayList<String> results = new ArrayList<String>();
			Matcher timer = null;
			Pattern p = Pattern.compile("\\d{4}-\\d{2}-\\d{2}\\b\\d{2}:\\d{2}:\\d{2}");
			int len = "AppHomeLib".length();
			int beg = 0;
			while((linetext=br.readLine())!=null){
				if(( linetext.indexOf("AppHomeLib")!=-1 || linetext.indexOf("AppInstallManager")!=-1 ) && linetext.indexOf("session")==-1 && linetext.indexOf("[com.vitria.m3oui.model.PerformanceLogger]")!=-1){
					results.add(linetext);
				}
			}
			writeLog(destpath, results);
		}catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void readSlaModelPerformanceLog(String filepath, int count){
		try{
			BufferedReader br = new BufferedReader(new FileReader(filepath));
			String linetext = null;
			Matcher timeMatcher = null;
			Pattern p = Pattern.compile("\\d+\\sms");
			int expect = count;
			double total = 0;
			while((linetext=br.readLine())!=null){
				if(linetext.indexOf("SlaModel.onContextStarted")!=-1){
					timeMatcher = p.matcher(linetext);
					if(timeMatcher.find()){
						String temp = timeMatcher.group();
						temp = temp.substring(0,temp.indexOf("ms")-1);
						int time = Integer.parseInt(temp);
						count--;
						total+=time;
					}
				}
			}
			if(count>0){
				System.out.println("Expect count is "+expect+" , less "+count);
			}
			System.out.println("Average value is "+total/(expect-count)+" for "+(expect-count)+" times");
			br.close();
		}catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @param args 
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String filepath="D:/work/FeaturesTestOwned/applifecycle/performance/120405/server.log";
		LogFetcher fetcher = new LogFetcher();
		//fetcher.readTokenLog("D:/work/main/yoda/export/home/jboss/server/vtba/log/server.log", "D:/result.txt");
		//fetcher.readSlaModelPerformanceLog(filepath, 10);
		//fetcher.readAppPerformanceLog(filepath, "D:/result.txt");
		fetcher.handleLog(1, 15, "D:/result.txt");
	}

}
